package com.example.imitationoftodaysheadlines;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private List<NewsBean> newsBeanList;

    public RecyclerViewAdapter(List<NewsBean> newsBeanList) {
        this.newsBeanList = newsBeanList;
    }

   // 创建界面控制器
    @NonNull // 作用 不能为空
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

       View view= LayoutInflater.from( parent.getContext() ).inflate(R.layout.news_item,parent,false);
       RecyclerViewAdapter.ViewHolder viewHolder=new RecyclerViewAdapter.ViewHolder(view);
       return viewHolder;
    }
    // 绑定数据
    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        NewsBean newsBean=newsBeanList.get(position);
        holder.title.setText(newsBean.getTitle());
        holder.tv_name.setText(newsBean.getName());
        holder.tv_comment.setText(newsBean.getComment());
        holder.tv_time.setText(newsBean.getTime());
        holder.iv_img.setImageResource(newsBean.getImgList());
        holder.iv_top.setVisibility(View.GONE);
        if (position==0){
            holder.iv_top.setVisibility(View.VISIBLE);
        }else {
            holder.iv_top.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),newsBean.getTitle(),Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return newsBeanList.size();
    }
    // 界面控制器
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_img,iv_top;
        TextView  title,tv_name,tv_comment,tv_time;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_top=itemView.findViewById(R.id.iv_top);
            iv_img=itemView.findViewById(R.id.iv_img);
            title=itemView.findViewById(R.id.tv_title);
            tv_name=itemView.findViewById(R.id.tv_name);
            tv_comment=itemView.findViewById(R.id.tv_comment);
            tv_time=itemView.findViewById(R.id.tv_time);
        }
    }
}
